package com.zqh.authority.controller;

import com.alibaba.fastjson.JSONObject;
import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.sun.jersey.api.client.Client;
import com.zqh.authority.service.SysUserService;
import com.zqh.authority.util.Upload;
import com.zqh.common.annotation.SysLog;
import com.zqh.common.constant.SystemConstant;
import com.zqh.common.controller.AbstractController;
import com.zqh.common.entity.Page;
import com.zqh.common.entity.R;
import com.zqh.common.entity.SysUserEntity;
import com.zqh.common.utils.MD5Utils;
import com.zqh.common.validator.LengthValidator;
import com.zqh.common.validator.NotNullValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author wangzheng
 * @email wangzheng19851019@163.com
 * @url www.sungohealth.com
 * @date 2017年8月8日 下午9:04:59
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
	
	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * 用户列表
	 * @param params
	 * @return
	 */
	@RequestMapping("/list1")
	public Page<SysUserEntity> list(@RequestParam Map<String, Object> params) {
		long a= getUserId();
		if(a != SystemConstant.SUPER_ADMIN) {
			params.put("userIdCreate", getUserId());
		}
		return sysUserService.listUser(params);
	}
	
	/**
	 * 获取登录的用户信息
	 */
	@RequestMapping("/info")
	public R info(){

		return R.ok().put("user", getUser());
	}

	/**
	 * 获取登录的用户所属机构
	 */
	@RequestMapping("/userOrg")
	public R userOrg(){
		HashMap map = new HashMap();
		map.put("orgId",getUser().getOrgId());
		map.put("orgToken",MD5Utils.encryptMd5("sungo"+getUser().getOrgId()));
		return R.ok().put("data", map);
	}
	
	/**
	 * 用户权限
	 * @return
	 */
	@RequestMapping("/perms")
	public R listUserPerms() {
		return sysUserService.listUserPerms(getUserId());
	}
	
	/**
	 * 新增用户
	 * @param user
	 * @return
	 */
	@SysLog("新增用户")
	@RequestMapping("/save1")
	public R save(SysUserEntity user) {
		ComplexResult result = FluentValidator.checkAll()
				.on(user.getUsername(), new NotNullValidator("用户名"))
				.on(String.valueOf(user.getOrgId()), new NotNullValidator("所属机构"))
				.on(user.getPassword(), new LengthValidator(1,100,"密码"))
				.doValidate()
				.result(ResultCollectors.toComplex());
		if (!result.isSuccess()) {
			return R.error(result.getErrors());
		}
		user.setUserIdCreate(getUserId());
		return sysUserService.saveUser(user);
	}
	
	/**
	 * 根据id查询详情
	 * @param userId
	 * @return
	 */
	@RequestMapping("/infoUser")
	public R getById(Long userId) {
		return sysUserService.getUserById(userId);
	}
	
	/**
	 * 修改用户
	 * @param user
	 * @return
	 */
	@SysLog("修改用户")
	@RequestMapping("/update1")
	public R update( SysUserEntity user) {

		return sysUserService.updateUser(user);
	}

	@RequestMapping("/uploadHead1")
	public R uploadHead(HttpServletRequest request, HttpServletResponse response) {
			MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile file = multipartRequest.getFile("picUrl");
			return sysUserService.uploadHead(file);
	}


	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@SysLog("删除用户")
	@RequestMapping("/remove1")
	public R batchRemove(Long[] id) {
		return sysUserService.batchRemove(id);
	}
	
	/**
	 * 用户修改密码
	 * @param pswd
	 * @param newPswd
	 * @return
	 */
	@SysLog("修改密码")
	@RequestMapping("/updatePswd")
	public R updatePswdByUser(String pswd, String newPswd) {
		SysUserEntity user = getUser();
		//原密码
		user.setPassword(pswd);
		//邮箱临时存储新密码
		user.setEmail(newPswd);
		return sysUserService.updatePswdByUser(user);
	}
	
	/**
	 * 启用账户
	 * @param id
	 * @return
	 */
	@SysLog("启用账户")
	@RequestMapping("/enable")
	public R updateUserEnable(Long[] id) {
		return sysUserService.updateUserEnable(id);
	}
	
	/**
	 * 禁用账户
	 * @param id
	 * @return
	 */
	@SysLog("禁用账户")
	@RequestMapping("/disable")
	public R updateUserDisable(Long[] id) {
		return sysUserService.updateUserDisable(id);
	}
	
	/**
	 * 重置密码
	 * @param user
	 * @return
	 */
	@SysLog("重置密码")
	@RequestMapping("/reset")
	public R updatePswd(@RequestBody SysUserEntity user) {
		return sysUserService.updatePswd(user);
	}
}
